package tako.wisers.com.android.apis.handler;

import java.util.List;

import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.activity.Tako;
import tako.wisers.com.android.apis.adapter.ListManager;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.event.EventBus;
import tako.wisers.com.android.apis.event.TextEvent;
import tako.wisers.com.android.apis.item.TextItem;
import tako.wisers.com.android.apis.location.GPSManager;
import tako.wisers.com.android.apis.parser.html.HTMLListParser;
import tako.wisers.com.android.apis.util.Global;

public class TextHandler extends TakoHandler implements Handler<TextEvent> {
	public TextHandler(Tako tako) {
		super(tako);
	}

	private String keyword;

	@Override
	public void execute(TextEvent event) {
		if (tako.getBottomDisplay() != Global.LIST_DISPLAY) {
			Event lEvent = new Event(R.id.list, Event.EVENT_SWITCH, false);
			EventBus.getInstance().executeEvent(lEvent);
		}
		if (event.isSearchBykeyword()) {
			AutoCompleteTextView editorText = (AutoCompleteTextView) tako
					.findViewById(R.id.keyword);
			keyword = editorText.getText().toString();
			String countryCode = null;
			if (editorText.getVisibility() != AutoCompleteTextView.VISIBLE
					|| keyword == null || keyword.equals("")) {
				return;
			}
			List<TextItem> items = new HTMLListParser().getNewsHeadlines(
					keyword, countryCode);
			if (items == null || items.size() == 0) {
				mhandler.sendMessage(mhandler.obtainMessage(SEARCH_FASILER));
			} else {
//				ListManager.getInstance(tako).loadNews(items);
				mhandler.sendMessage(mhandler.obtainMessage(SEARCH_SUCCESS));
			}
		}else{
			
		}
		
		System.gc();
	}

	private android.os.Handler mhandler = new android.os.Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			tako.dismissDialog();
			if (msg.what == SEARCH_FASILER)
				tako.dialogShow(Global.WAITING_DIALOG);
		}
	};

}
